String.prototype.isIDCard = function () {
    var iSum = 0;
    var info = '';
    var sId = this;
    var aCity = {
        11: '',
        12: '',
        13: '',
        14: '',
        15: '',
        21: '',
        22: '',
        23: '',
        31: '',
        32: '',
        33: '',
        34: '',
        35: '',
        36: '',
        37: '',
        41: '',
        42: '',
        43: '',
        44: '',
        45: '',
        46: '',
        50: '',
        51: '',
        52: '',
        53: '',
        54: '',
        61: '',
        62: '',
        63: '',
        64: '',
        65: '',
        71: '',
        81: '',
        82: '',
        91: ''
    }
    if (!/^\d{15}$/i.test(sId) && !/^\d{17}(\d|x)$/i.test(sId)) {
        return false
    }
    sId = sId.replace(/x$/i, 'a')
    if (aCity[parseInt(sId.substr(0, 2))] == null) {
        return false
    }
    if (sId.length == 15) {
        var sBirthday =
            '19' +
            sId.substr(6, 2) +
            '-' +
            Number(sId.substr(8, 2)) +
            '-' +
            Number(sId.substr(10, 2))
    } else {
        var sBirthday =
            sId.substr(6, 4) +
            '-' +
            Number(sId.substr(10, 2)) +
            '-' +
            Number(sId.substr(12, 2))
    }
    var d = new Date(sBirthday.replace(/-/g, '/'))
    if (
        sBirthday !=
        d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    ) {
        return false
    }
    if (sId.length == 15) return true
    for (var i = 17; i >= 0; i--) {
        iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11)
    }
    if (iSum % 11 != 1) {
        return false
    }
    return true
}
String.prototype.isTWIDCard = function () {
    var dict = {
        A: ['10', ''],
        B: ['11', ''],
        C: ['12', ''],
        D: ['13', ''],
        E: ['14', ''],
        F: ['15', ''],
        G: ['16', ''],
        H: ['17', ''],
        J: ['18', ''],
        K: ['19', ''],
        L: ['20', ''],
        M: ['21', ''],
        N: ['22', ''],
        P: ['23', ''],
        Q: ['24', ''],
        R: ['25', ''],
        S: ['26', ''],
        T: ['27', ''],
        U: ['28', ''],
        V: ['29', ''],
        X: ['30', ''],
        Y: ['31', ''],
        W: ['32', ''],
        Z: ['33', ''],
        I: ['34', ''],
        O: ['35', '']
    }
    var tId = this
    if (!/^[A-Z]\d{9}$/.test(tId)) return false
    var t = dict[tId.substr(0, 1)][0]
    var X1 = parseInt(t.substr(0, 1))
    var X2 = parseInt(t.substr(1, 1))
    var D = new Array(9)
    for (var i = 0; i < D.length; i++) D[i] = parseInt(tId.substr(i + 1, 1))
    var Y =
        X1 +
        9 * X2 +
        8 * D[0] +
        7 * D[1] +
        6 * D[2] +
        5 * D[3] +
        4 * D[4] +
        3 * D[5] +
        2 * D[6] +
        1 * D[7] +
        D[8]
    return Y % 10 == 0 ? true : false
}
String.prototype.isHKIDCard = function () {
    return /^[A-Z]\d{6,7}\([A\d]\)$/.test(this)
}
String.prototype.isAMIDCard = function () {
    return /^[157]\d{6}\(\d\)$/.test(this)
}
var chkType = [
    [
        'str',
        'int',
        'userName',
        'en',
        'cn',
        'email',
        'mobile',
        'landline',
        'float',
        'date',
        'post',
        'qq',
        'psw',
        'rePsw',
        'file',
        'sng',
        'mul',
        'ajax',
        'idcard',
        'fun',
        'reg'
    ],
    [
        /^\S+/,
        /^[-\+]?\d+$/,
        /^\w{3,16}$/,
        /^\w+$/,
        /^[\u0391-\uFFE5]+$/,
        /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
        /^((\+\d{2})?1\d{2})\d{8}$/,
        /^(([0\+]\d{2,3}-?)?(0\d{2,3})-?)?(\d{7,8})(-(\d{3,}))?$/,
        /^[-\+]?\d+(\.\d+)?$/,
        /^(\d{0,4}-)?\d{1,2}-\d{1,2}(\s+\d{1,2}(:\d{1,2})+)?$/,
        /^[1-9]\d{5}$/,
        /^[1-9]\d{4,11}$/,
        /^.{6,16}$/,
        /^.{6,16}$/,
        /^\S+/,
        /sng/,
        /mul/,
        /ajax/,
        /idcard/,
        /fun/,
        /reg/
    ],
]

export function chkMinMax(value, vMin, vMax) {
    var vMin = parseFloat(vMin);
    var vMax = parseFloat(vMax);
    value = parseFloat(value);
    var minFlag = isNaN(vMin) ? true : vMin <= value;
    var maxFlag = isNaN(vMax) ? true : vMax >= value;
    return minFlag && maxFlag
}

export function chkInt(value, vMin, vMax) {
    return /^[-\+]?\d+$/.test(value) && chkMinMax(value, vMin, vMax)
}

export function chkFloat(value, vMin, vMax) {
    return /^[-\+]?\d+(\.\d+)?$/.test(value) && chkMinMax(value, vMin, vMax)
}

export function chkEn(value) {
    return /^\w+$/.test(value)
}

export function chkCn(value) {
    return /^[\u0391-\uFFE5]+$/.test(value)
}

export function chkEmail(value) {
    return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(
        value
    )
}

export function chkMobile(value) {
    return /^(\+\d{2})?1[23456789]\d{9}$/.test(value)
}

export function chkIdcard(value) {
    return (
        value.isIDCard() ||
        value.isTWIDCard() ||
        value.isHKIDCard() ||
        value.isAMIDCard()
    )
}

export function chkStr(value, minLen, maxLen) {
    return /^\S+/.test(value) && chkMinMax(value.length, minLen, maxLen)
}

// 固定电话
export function chkLandline(value, minLen, maxLen) {
    return /^(([0\+]\d{2,3}-?)?(0\d{2,3})-?)?(\d{7,8})(-(\d{3,}))?$/.test(value)
}

//指定输入小数点后几位
export function appointNum(value, len) {
    if(value.toString().split(".")[1] != undefined){
        return value.toString().split(".")[1].length <= len;
    }
    return true;
}

//指定数字范围
export  function appointInt(value, minNum, maxNum) {
    if(parseFloat(value)>=minNum && parseFloat(value)<=maxNum) {
        return true;
    }
}

//去掉汉字  
export function removeChinese(strValue) {
    if(strValue!= null && strValue != ""){
        let reg = /[\u4e00-\u9fa5]/g;
        return strValue.replace(reg, "");
    }
    else
        return "";
}

//只提取汉字  
export function getChinese(strValue) {
    if(strValue!= null && strValue!= ""){
        let reg = /[\u4e00-\u9fa5]/g;
        if(strValue.match(reg)) return strValue.match(reg).join("");
        return "";
    }
    else
        return "";
}

// 判断是否全是汉字
export function needChinese(strValue) {
    if (!/^[\u4e00-\u9fa5]+$/gi.test(strValue)) return false;
    return true;
}

// 判断是数字且位数
export function floatType(numValue, len) {
    let re = /^[0-9]+.?[0-9]*$/;
    if (re.test(Number(numValue))) {
        let dis = numValue.toString().split(".")[1];
        if (dis) {
            if (dis.length <= len) return true;
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

//保留小数点后几位  
export function toFixed(strValue, num) {
    let re = new RegExp("([0-9]+.[0-9]{" + num + "})[0-9]*","g");
    return strValue.replace(re,"$1");
}

// 判断是否是整数类型
export function isInteger(obj) {
    return Number(obj) % 1 === 0
}

// 判断是否n的倍数
export function multipleIntenger(num, mul) {
    return Number(num) % mul === 0
}

// 判断组织机构代码
export function codeForm(num) {
    return /^([0-9]|[A-Z]){8}-([0-9]|[A-Z])$/.test(num);
}

//判断详细地址必须有数字
export function addressNum(text) {
    return /^[\u4e00-\u9fa5]+\D*\d+/.test(text);
}
